package com.zb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.SelectFeeProjectVo;
import com.zb.entity.SelectVo;
import com.zb.entity.ZjWorkFeeProject;
import com.zb.mapper.ZjWorkFeeProjectMapper;
import com.zb.result.PageUtil;
import com.zb.service.ZjWorkFeeDetailService;
import com.zb.service.ZjWorkFeeProjectService;
import com.zb.vo.WorkFeeProjectListVo;
import com.zb.vo.WorkFeeProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 工单费用项目 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-09-13
 */
@Service
public class ZjWorkFeeProjectServiceImpl extends ServiceImpl<ZjWorkFeeProjectMapper, ZjWorkFeeProject> implements ZjWorkFeeProjectService {
    @Resource
    private ZjWorkFeeProjectMapper workFeeProjectMapper;
    @Resource
    private ZjWorkFeeDetailService workFeeDetailService;

    // 新增或修改工单费用项目
    @Override
    public void save(Long custAccId, WorkFeeProjectVo workFeeProjectVo) {
        Assert.isTrue(null == workFeeProjectMapper.existByName(custAccId, workFeeProjectVo.getId(), workFeeProjectVo.getName()), "费用项目名称已重复");
        ZjWorkFeeProject feeProject = new ZjWorkFeeProject();
        BeanUtils.copyProperties(workFeeProjectVo, feeProject);
        feeProject.setDisabled(workFeeProjectVo.getStatus());
        if (null != feeProject.getId()) {
            this.updateById(feeProject);
            return;
        }
        feeProject.setId(IdWorker.getId());
        feeProject.setCustAccId(custAccId);
        this.save(feeProject);
    }

    // 获取工单费用项目列表
    @Override
    public Map<String, Object> getList(Long custAccId, SelectVo selectVo) {
        QueryWrapper<ZjWorkFeeProject> wrapper = new QueryWrapper<>();
        wrapper.eq("cust_acc_id", custAccId);
        wrapper.orderByDesc("gmt_update");
        if (StringUtils.isNotBlank(selectVo.getName())) {
            wrapper.like("name", selectVo.getName());
        }
        PageUtil.setPageLimit(selectVo);
        Page<ZjWorkFeeProject> page = this.page(new Page<>(selectVo.getPage(), selectVo.getLimit()), wrapper);
        List<WorkFeeProjectListVo> voList = page.getRecords().stream().map(fee -> {
            WorkFeeProjectListVo vo = new WorkFeeProjectListVo();
            BeanUtils.copyProperties(fee, vo);
            vo.setStatus(fee.getDisabled());
            vo.setCreateTime(fee.getGmtCreate());
            vo.setUpdateTime(fee.getGmtUpdate());
            return vo;
        }).toList();
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), voList);
    }

    // 根据费用项目ID获取费用详情
    @Override
    public WorkFeeProjectVo getDetail(Long id) {
        ZjWorkFeeProject project = this.getById(id);
        Assert.isTrue(null != project, "费用项目参数["+ id + "]的数据不存在");
        WorkFeeProjectVo vo = new WorkFeeProjectVo();
        BeanUtils.copyProperties(project, vo);
        vo.setStatus(project.getDisabled());
        return vo;
    }

    // 根据费用项目ID批量启用或禁用费用项目
    @Override
    public void updateDisabledBatchById(List<Long> idList, boolean status) {
        workFeeProjectMapper.updateDisabledBatchById(idList, status);
    }

    // 根据费用项目ID批量删除费用项目
    @Override
    public void deleteBatchByid(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        // 查询关联工单的费用项目
        List<Long> ids =  workFeeDetailService.getByWorkFeeProjectId(idList);
        if (CollectionUtil.isNotEmpty(ids)) {
            QueryWrapper<ZjWorkFeeProject> wrapper = new QueryWrapper<>();
            wrapper.select("name");
            wrapper.in("id", ids);
            Assert.isTrue(false, "费用项目["+this.list(wrapper).stream().map(ZjWorkFeeProject::getName).collect(Collectors.joining("、"))+"]已与工单关联，不可删除");
        }
        this.removeByIds(idList);
    }

    // 工单费用项目下拉列表查询
    @Override
    public Map<String, Object> workFeeProjectPageSelect(Long custAccId, SelectVo selectVo) {
        QueryWrapper<ZjWorkFeeProject> wrapper = new QueryWrapper<>();
        wrapper.select("id", "name", "price", "is_update_price AS updatePrice");
        wrapper.eq("cust_acc_id", custAccId).eq("is_disabled", false);
        if (StringUtils.isNotBlank(selectVo.getName())) {
            wrapper.like("name", selectVo.getName());
        }
        PageUtil.setSelectPageLimit(selectVo);
        Page<ZjWorkFeeProject> page = this.page(new Page<>(selectVo.getPage(), selectVo.getLimit()), wrapper);
        List<SelectFeeProjectVo> voList = page.getRecords().stream().map(fee -> {
            return new SelectFeeProjectVo(fee.getId(), fee.getName(), fee.getPrice(), fee.getUpdatePrice());
        }).toList();
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), voList);
    }

    // 获取移动端工单服务费用下拉数据
    @Override
    public List<SelectFeeProjectVo> getMobileOrdeServiceFeeSelect(Long custAccId) {
        QueryWrapper<ZjWorkFeeProject> wrapper = new QueryWrapper<>();
        wrapper.select("id", "name", "price", "is_update_price AS updatePrice");
        wrapper.eq("cust_acc_id", custAccId).eq("is_disabled", false);
        return this.list(wrapper).stream().map(fee -> {
            return new SelectFeeProjectVo(fee.getId(), fee.getName(), fee.getPrice(), fee.getUpdatePrice());
        }).toList();
    }
}
